package com.hk.servlet;

import com.hk.model.Display;
import com.hk.model.ProductMenu;
import com.hk.service.IDisplayService;
import com.hk.service.IProductMenuService;
import com.hk.service.impl.DisplayServiceImpl;
import com.hk.service.impl.ProductMenuServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Author: KangWang
 * Date: 2021/5/17 10:20
 * State：Servlet 数据请求转发 控制层
 */
@WebServlet("/productDisplay")
public class ProductDisplayServlet extends HttpServlet {

    IProductMenuService iProductMenuService = new ProductMenuServiceImpl();
    IDisplayService iDisplayService = new DisplayServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        设置UTF-8
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        获取项目路径
        String path = req.getServletContext().getRealPath("");
        HttpSession session = req.getSession();
        //        获取产品菜单数据
        List<ProductMenu> productMenus = iProductMenuService.findByParentId(path, 0);
//        获取产品展示列表
        List<Display> displays = iDisplayService.findAllDisplay(path);
//        存储数据
        session.setAttribute("productMenus",productMenus);
        session.setAttribute("displays",displays);
//        页面转发
        req.getRequestDispatcher("/jsp/product_display.jsp").forward(req,resp);
//        页面重定向
//        resp.sendRedirect("/jsp/home.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
